package com.cornergj.interfaces.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.cornergj.model.Entity.CommonUserCriteria;
import com.cornergj.model.Entity.UserBasicInfoCriteria;
import com.cornergj.model.Entity.Value;
import com.cornergj.model.OrderService;
import com.cornergj.model.UserBasicInfo;
import com.cornergj.response.ErrorCode;
import com.cornergj.response.JsonResult;
import com.cornergj.service.CommonUserService;
import com.cornergj.service.OrderServiceService;
import com.cornergj.service.UserBasicInfoService;
import com.cornergj.uitl.ALiYunSmsUtil;
import com.cornergj.uitl.CryptUtil;
import com.cornergj.uitl.PropertyUtil;



/**
 * Title:
 * Model:
 * Description:
 * @author 郑耿杰
 * @date 2018年1月31日
 */
@Controller
@RequestMapping("/submitorder")
public class OrderController {
	
	@Autowired
	private CommonUserService commonUserService;
	@Autowired
	private UserBasicInfoService userBasicInfoService;
	@Autowired
	private OrderServiceService orderServiceService;
	
	private final String[] codeArray={"A","B","W","S","P","X","Y","Z","J","G"};
	/*
	 * 訂單
	 */
	@RequestMapping(value="/makeOrder",method= RequestMethod.POST)
	@ResponseBody
    public JsonResult submitOrder(HttpServletRequest request,HttpServletResponse response,OrderService order) {
		JsonResult result=new JsonResult();
		UserBasicInfo userinfo=(UserBasicInfo) request.getSession().getAttribute("COMMONUSER");
		if(userinfo!=null){
			StringBuffer orderbuffer=new StringBuffer();
			Random randow=new Random();
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
			String nowTime=dateFormat.format(new Date());
			for(int i=0;i<2;i++){
				int ran=randow.nextInt(10);
				orderbuffer.append(codeArray[ran]);
			}
			orderbuffer.append(nowTime);
			for(int j=0;j<4;j++){
				orderbuffer.append(randow.nextInt(10));
			}
			order.setOrderCode(orderbuffer.toString());
			order.setUserId(userinfo.getId());
			order.setArrange(0);
			order.setUserId(userinfo.getId());
			order.setArrange(0);
			order.setPay(0);
			order.setCreateTime(new Date());
			orderServiceService.save(order);
			result.setData(orderbuffer.toString());
		}else{
			result.addErrorCode(ErrorCode.SUBMIT_ORDER_FAIL);
		}
		return result;
	}
	
	
	
}
